package com.woniuxy.ticketauth.controller;

import com.woniuxy.commnes.entity.ResponseResult;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;
/**
 * @ClassName AuthController
 * @Description 接受网关请求
 * @Author zx
 * @Date 2021-03-05 10:40:05
 * @Version 1.0
 */
@RestController
public class AuthController {
    @Value("${server.port}")
    private int port;

    @RequestMapping("auth")
    public ResponseResult<Void> auth(String jwt, String url) throws URISyntaxException {
        //发送HTTP请求到本项目，会经过Shiro过滤器
        URI uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(port).setPath(url).build();
        // build http headers
        HttpHeaders headers = new HttpHeaders();
        headers.add("jwt", jwt);
        RestTemplate restTemplate = new RestTemplate();
        // send request and get response
        ResponseResult result = restTemplate.postForObject(uri, new HttpEntity<String>(headers), ResponseResult.class);
        return result;
    }
}
